class SongsController < ApplicationController
include AWS::S3

BUCKET = 's3tutorialmusic_442'

  def index  	
  	#Bucket.create('jukebox_420Tester') <- this is kinda cool, lets you create a bucket
  	#@songs = Bucket.find('s3tutorialmusic').objects <- this is usable but its a default tutorial
  	@songs = Bucket.find(BUCKET).objects
  end

  def upload
 		begin
			AWS::S3::S3Object.store(sanitize_filename(params[:mp3file].original_filename), params[:mp3file].read, BUCKET, :access => :public_read)
			redirect_to root_path
		rescue
			render :text => "Couldn't complete the upload"
		end
	end

  def delete
  	if (params[:song])
    	AWS::S3::S3Object.find(params[:song], BUCKET).delete
    	redirect_to root_path
		else
    	render :text => "No song was found to delete!"
		end
  end
  
  def sanitize_filename(file_name)
    just_filename = File.basename(file_name)
    just_filename.sub(/[^\w\.\-]/,'_')
  end
  
end
